summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/fssystem/fssystem_bucket_tree.h
blob: 3a5e21d1a4444160a8c8aea7ab2645d873a63e46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <mutex>

#include "common/alignment.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/literals.h"

#include "core/file_sys/vfs/vfs.h"
#include "core/hle/result.h"

namespace FileSys {

using namespace Common::Literals;

class BucketTree {
    YUZU_NON_COPYABLE(BucketTree);
    YUZU_NON_MOVEABLE(BucketTree);

public:
    static constexpr u32 Magic = Common::MakeMagic('B', 'K', 'T', 'R');
    static constexpr u32 Version = 1;

    static constexpr size_t NodeSizeMin = 1_KiB;
    static constexpr size_t NodeSizeMax = 512_KiB;

public:
    class Visitor;

    struct Header {
        u32 magic;
        u32 version;
        s32 entry_count;
        s32 reserved;

        void Format(s32 entry_count);
        Result Verify() const;
    };
    static_assert(std::is_trivial_v<Header>);
    static_assert(sizeof(Header) == 0x10);

    struct NodeHeader {
        s32 index;
        s32 count;
        s64 offset;

        Result Verify(s32 node_index, size_t node_size, size_t entry_size) const;
    };
    static_assert(std::is_trivial_v<NodeHeader>);
    static_assert(sizeof(NodeHeader) == 0x10);

    struct Offsets {
        s64 start_offset;
        s64 end_offset;

        constexpr bool IsInclude(s64 offset) const {
            return this->start_offset <= offset && offset < this->end_offset;
        }

        constexpr bool IsInclude(s64 offset, s64 size) const {
            return size > 0 && this->start_offset <= offset && size <= (this->end_offset - offset);
        }
    };
    static_assert(std::is_trivial_v<Offsets>);
    static_assert(sizeof(Offsets) == 0x10);

    struct OffsetCache {
        Offsets offsets;
        std::mutex mutex;
        bool is_initialized;

        OffsetCache() : offsets{-1, -1}, mutex(), is_initialized(false) {}
    };

    class ContinuousReadingInfo {
    public:
        constexpr ContinuousReadingInfo() : m_read_size(), m_skip_count(), m_done() {}

        constexpr void Reset() {
            m_read_size = 0;
            m_skip_count = 0;
            m_done = false;
        }

        constexpr void SetSkipCount(s32 count) {
            ASSERT(count >= 0);
            m_skip_count = count;
        }
        constexpr s32 GetSkipCount() const {
            return m_skip_count;
        }
        constexpr bool CheckNeedScan() {
            return (--m_skip_count) <= 0;
        }

        constexpr void Done() {
            m_read_size = 0;
            m_done = true;
        }
        constexpr bool IsDone() const {
            return m_done;
        }

        constexpr void SetReadSize(size_t size) {
            m_read_size = size;
        }
        constexpr size_t GetReadSize() const {
            return m_read_size;
        }
        constexpr bool CanDo() const {
            return m_read_size > 0;
        }

    private:
        size_t m_read_size;
        s32 m_skip_count;
        bool m_done;
    };

private:
    class NodeBuffer {
        YUZU_NON_COPYABLE(NodeBuffer);

    public:
        NodeBuffer() : m_header() {}

        ~NodeBuffer() {
            ASSERT(m_header == nullptr);
        }

        NodeBuffer(NodeBuffer&& rhs) : m_header(rhs.m_header) {
            rhs.m_header = nullptr;
        }

        NodeBuffer& operator=(NodeBuffer&& rhs) {
            if (this != std::addressof(rhs)) {
                ASSERT(m_header == nullptr);

                m_header = rhs.m_header;

                rhs.m_header = nullptr;
            }
            return *this;
        }

        bool Allocate(size_t node_size) {
            ASSERT(m_header == nullptr);

            m_header = ::operator new(node_size, std::align_val_t{sizeof(s64)});

            // ASSERT(Common::IsAligned(m_header, sizeof(s64)));

            return m_header != nullptr;
        }

        void Free(size_t node_size) {
            if (m_header) {
                ::operator delete(m_header, std::align_val_t{sizeof(s64)});
                m_header = nullptr;
            }
        }

        void FillZero(size_t node_size) const {
            if (m_header) {
                std::memset(m_header, 0, node_size);
            }
        }

        NodeHeader* Get() const {
            return reinterpret_cast<NodeHeader*>(m_header);
        }

        NodeHeader* operator->() const {
            return this->Get();
        }

        template <typename T>
        T* Get() const {
            static_assert(std::is_trivial_v<T>);
            static_assert(sizeof(T) == sizeof(NodeHeader));
            return reinterpret_cast<T*>(m_header);
        }

    private:
        void* m_header;
    };

private:
    static constexpr s32 GetEntryCount(size_t node_size, size_t entry_size) {
        return static_cast<s32>((node_size - sizeof(NodeHeader)) / entry_size);
    }

    static constexpr s32 GetOffsetCount(size_t node_size) {
        return static_cast<s32>((node_size - sizeof(NodeHeader)) / sizeof(s64));
    }

    static constexpr s32 GetEntrySetCount(size_t node_size, size_t entry_size, s32 entry_count) {
        const s32 entry_count_per_node = GetEntryCount(node_size, entry_size);
        return Common::DivideUp(entry_count, entry_count_per_node);
    }

    static constexpr s32 GetNodeL2Count(size_t node_size, size_t entry_size, s32 entry_count) {
        const s32 offset_count_per_node = GetOffsetCount(node_size);
        const s32 entry_set_count = GetEntrySetCount(node_size, entry_size, entry_count);

        if (entry_set_count <= offset_count_per_node) {
            return 0;
        }

        const s32 node_l2_count = Common::DivideUp(entry_set_count, offset_count_per_node);
        ASSERT(node_l2_count <= offset_count_per_node);

        return Common::DivideUp(entry_set_count - (offset_count_per_node - (node_l2_count - 1)),
                                offset_count_per_node);
    }

public:
    BucketTree()
        : m_node_storage(), m_entry_storage(), m_node_l1(), m_node_size(), m_entry_size(),
          m_entry_count(), m_offset_count(), m_entry_set_count(), m_offset_cache() {}
    ~BucketTree() {
        this->Finalize();
    }

    Result Initialize(VirtualFile node_storage, VirtualFile entry_storage, size_t node_size,
                      size_t entry_size, s32 entry_count);
    void Initialize(size_t node_size, s64 end_offset);
    void Finalize();

    bool IsInitialized() const {
        return m_node_size > 0;
    }
    bool IsEmpty() const {
        return m_entry_size == 0;
    }

    Result Find(Visitor* visitor, s64 virtual_address);
    Result InvalidateCache();

    s32 GetEntryCount() const {
        return m_entry_count;
    }

    Result GetOffsets(Offsets* out) {
        // Ensure we have an offset cache.
        R_TRY(this->EnsureOffsetCache());

        // Set the output.
        *out = m_offset_cache.offsets;
        R_SUCCEED();
    }

public:
    static constexpr s64 QueryHeaderStorageSize() {
        return sizeof(Header);
    }

    static constexpr s64 QueryNodeStorageSize(size_t node_size, size_t entry_size,
                                              s32 entry_count) {
        ASSERT(entry_size >= sizeof(s64));
        ASSERT(node_size >= entry_size + sizeof(NodeHeader));
        ASSERT(NodeSizeMin <= node_size && node_size <= NodeSizeMax);
        ASSERT(Common::IsPowerOfTwo(node_size));
        ASSERT(entry_count >= 0);

        if (entry_count <= 0) {
            return 0;
        }
        return (1 + GetNodeL2Count(node_size, entry_size, entry_count)) *
               static_cast<s64>(node_size);
    }

    static constexpr s64 QueryEntryStorageSize(size_t node_size, size_t entry_size,
                                               s32 entry_count) {
        ASSERT(entry_size >= sizeof(s64));
        ASSERT(node_size >= entry_size + sizeof(NodeHeader));
        ASSERT(NodeSizeMin <= node_size && node_size <= NodeSizeMax);
        ASSERT(Common::IsPowerOfTwo(node_size));
        ASSERT(entry_count >= 0);

        if (entry_count <= 0) {
            return 0;
        }
        return GetEntrySetCount(node_size, entry_size, entry_count) * static_cast<s64>(node_size);
    }

private:
    template <typename EntryType>
    struct ContinuousReadingParam {
        s64 offset;
        size_t size;
        NodeHeader entry_set;
        s32 entry_index;
        Offsets offsets;
        EntryType entry;
    };

private:
    template <typename EntryType>
    Result ScanContinuousReading(ContinuousReadingInfo* out_info,
                                 const ContinuousReadingParam<EntryType>& param) const;

    bool IsExistL2() const {
        return m_offset_count < m_entry_set_count;
    }
    bool IsExistOffsetL2OnL1() const {
        return this->IsExistL2() && m_node_l1->count < m_offset_count;
    }

    s64 GetEntrySetIndex(s32 node_index, s32 offset_index) const {
        return (m_offset_count - m_node_l1->count) + (m_offset_count * node_index) + offset_index;
    }

    Result EnsureOffsetCache();

private:
    mutable VirtualFile m_node_storage;
    mutable VirtualFile m_entry_storage;
    NodeBuffer m_node_l1;
    size_t m_node_size;
    size_t m_entry_size;
    s32 m_entry_count;
    s32 m_offset_count;
    s32 m_entry_set_count;
    OffsetCache m_offset_cache;
};

class BucketTree::Visitor {
    YUZU_NON_COPYABLE(Visitor);
    YUZU_NON_MOVEABLE(Visitor);

public:
    constexpr Visitor()
        : m_tree(), m_entry(), m_entry_index(-1), m_entry_set_count(), m_entry_set{} {}
    ~Visitor() {
        if (m_entry != nullptr) {
            ::operator delete(m_entry, m_tree->m_entry_size);
            m_tree = nullptr;
            m_entry = nullptr;
        }
    }

    bool IsValid() const {
        return m_entry_index >= 0;
    }
    bool CanMoveNext() const {
        return this->IsValid() && (m_entry_index + 1 < m_entry_set.info.count ||
                                   m_entry_set.info.index + 1 < m_entry_set_count);
    }
    bool CanMovePrevious() const {
        return this->IsValid() && (m_entry_index > 0 || m_entry_set.info.index > 0);
    }

    Result MoveNext();
    Result MovePrevious();

    template <typename EntryType>
    Result ScanContinuousReading(ContinuousReadingInfo* out_info, s64 offset, size_t size) const;

    const void* Get() const {
        ASSERT(this->IsValid());
        return m_entry;
    }

    template <typename T>
    const T* Get() const {
        ASSERT(this->IsValid());
        return reinterpret_cast<const T*>(m_entry);
    }

    const BucketTree* GetTree() const {
        return m_tree;
    }

private:
    Result Initialize(const BucketTree* tree, const BucketTree::Offsets& offsets);

    Result Find(s64 virtual_address);

    Result FindEntrySet(s32* out_index, s64 virtual_address, s32 node_index);
    Result FindEntrySetWithBuffer(s32* out_index, s64 virtual_address, s32 node_index,
                                  char* buffer);
    Result FindEntrySetWithoutBuffer(s32* out_index, s64 virtual_address, s32 node_index);

    Result FindEntry(s64 virtual_address, s32 entry_set_index);
    Result FindEntryWithBuffer(s64 virtual_address, s32 entry_set_index, char* buffer);
    Result FindEntryWithoutBuffer(s64 virtual_address, s32 entry_set_index);

private:
    friend class BucketTree;

    union EntrySetHeader {
        NodeHeader header;
        struct Info {
            s32 index;
            s32 count;
            s64 end;
            s64 start;
        } info;
        static_assert(std::is_trivial_v<Info>);
    };
    static_assert(std::is_trivial_v<EntrySetHeader>);

    const BucketTree* m_tree;
    BucketTree::Offsets m_offsets;
    void* m_entry;
    s32 m_entry_index;
    s32 m_entry_set_count;
    EntrySetHeader m_entry_set;
};

} // namespace FileSys